#pragma once
#include <iostream>
#include <string>
#include<cstring>
#include"Log.hpp"
#include"Init.hpp"

extern Log lg;
Init init;
class Task
{
public:
    Task(int sockfd, std::string clientip, uint16_t clientport)
        : sockfd_(sockfd), clientip_(clientip), clientport_(clientport)
    {}

    Task()
    {}

    void run()
    {
        char buffer[4096];

        // 把客户端发送给服务器的信息读取到buffer中
        ssize_t n = read(sockfd_, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = 0; // 当成字符串
            std::cout << "client key# " << buffer << std::endl;
            // 在把读到的信息发送给客户端
            std::string string_echo = init.tranlatin(buffer);
            n = write(sockfd_, string_echo.c_str(), string_echo.size());
            if(n < 0)
            {
                lg(Warning, "write error, error : %d, errstring: %s", errno, strerror(errno));
            }
        }
        else if (n == 0) // 客户端退出
        {
            lg(Info, "%s:%d quit, server close sockfd:%d", clientip_.c_str(), clientport_, sockfd_);
        }
        else // 读取失败
        {
            lg(Warning, "read error, sockfd:%d, client ip: %s, client port: %d", sockfd_, clientip_.c_str(), clientport_);
        }
        close(sockfd_);
    }
    void operator()()
    {
        run();
    }
    std::string GetResult()
    {
    }
    std::string GetTask()
    {
    }
    ~Task()
    {
    }

private:
    int sockfd_;
    std::string clientip_;
    uint16_t clientport_;
};